package com.cheyun.netsalev3.viewmodel

import android.content.Intent
import android.view.View
import android.widget.DatePicker
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.ObservableField
import androidx.lifecycle.MutableLiveData
import com.cheyun.netsalev3.MyApplication
import com.cheyun.netsalev3.bean.CarBrandParam
import com.cheyun.netsalev3.bean.DialogParam
import com.cheyun.netsalev3.bean.KeyValueParam
import com.cheyun.netsalev3.utils.MySharedPreferences
import com.cheyun.netsalev3.view.StockSearchResultActivity
import com.cheyun.netsalev3.view.choosecar.ChooseCarActivity
import com.cheyun.netsalev3.widget.DatePickerFragment
import com.cheyun.netsalev3.widget.MyDialog
import java.util.HashSet

class StockSearchActivityViewModel : BaseViewModel() {

    var listItem = MutableLiveData<List<KeyValueParam>>()
    //一级
    var typeShow1 = ObservableField<KeyValueParam>()
    var position1 = -1
    //二级
    var typeShow2 = ObservableField<DialogParam>(DialogParam("", ""))
    var position2 = -1
    private var infotype: Array<KeyValueParam>

    init {

        var baseData = MySharedPreferences.getBaseData()

        //线索拥有者
        var owneruidlist = ArrayList<DialogParam>()
        baseData?.member?.apply {
            for (i in this) {
                var item = DialogParam(i.realname, i.uid.toString())
                owneruidlist.add(item)
            }
        }


        //拟购车辆颜色
        var exteriorlist = ArrayList<DialogParam>()
        baseData?.exterior?.apply {
            for (i in this) {
                var item = DialogParam(i.title, i.id.toString())
                exteriorlist.add(item)
            }
        }
        //拟购内饰颜色
        var interiorlist = ArrayList<DialogParam>()
        baseData?.interior?.apply {
            for (i in this) {
                var item = DialogParam(i.title, i.id.toString())
                interiorlist.add(item)
            }
        }


        //库存状态
        var statelist = arrayListOf(
                DialogParam("在途车辆", "1"),
                DialogParam("预定车辆", "2"),
                DialogParam("在库车辆", "3"),
                DialogParam("出库车辆", "4"),
                DialogParam("配车车辆", "5")
        )
        //车型代号
        var typecodelist = ArrayList<DialogParam>()
        baseData?.typecode?.apply {
            for (i in this) {
                var item = DialogParam(i.title, i.id.toString())
                typecodelist.add(item)
            }
        }
        //库存来源
        var supplylist = ArrayList<DialogParam>()
        baseData?.supply?.apply {
            for (i in this) {
                var item = DialogParam(i.title, i.id.toString())
                supplylist.add(item)
            }
        }
        //存放仓库
        var warehouselist = ArrayList<DialogParam>()
        baseData?.warehouse?.apply {
            for (i in this) {
                var item = DialogParam(i.title, i.id.toString())
                warehouselist.add(item)
            }
        }

        infotype = arrayOf(
                //type2输入框类型 1选择类型框
                KeyValueParam("库存状态", statelist, "state"),
                KeyValueParam("品牌", 1, "brand"),
                KeyValueParam("车系", 1, "series"),
                KeyValueParam("车型", 1, "spec"),
                KeyValueParam("车身颜色", exteriorlist, "exterior"),
                KeyValueParam("内饰颜色", interiorlist, "interior"),
                KeyValueParam("车架号", 2, "vin"),
                KeyValueParam("发动机号", 2, "engineno"),
                KeyValueParam("车型代号", typecodelist, "typecode"),
                KeyValueParam("合格证号", 2, "certificatno"),
                KeyValueParam("批次号", 2, "lotno"),
                KeyValueParam("库存来源", supplylist, "supply"),
                KeyValueParam("存放仓库", warehouselist, "warehouse"),
                KeyValueParam("库位", 2, "location"),
                KeyValueParam("座位数", 2, "seat"),
                KeyValueParam("客户编号", 2, "iorderid"),
                KeyValueParam("客户姓名", 2, "uname"),
                KeyValueParam("客户电话", 2, "phone"),
                KeyValueParam("订单号", 2, "ordernum"),
                KeyValueParam("线索拥有者", owneruidlist, "owneruid")
        )

    }

    /**
     * 选中类型1
     */
    fun onClickType(view: View) {
        var content = view.context

        if (content is AppCompatActivity) {
            //深拷贝
            var list = ArrayList<DialogParam>()
            for (it in infotype) {
                var item = DialogParam(it.key, it.idname)
                item.IsSelect = it.IsSelect
                item.ikey = it.ikey
                list.add(item)
            }
            val newFragment = MyDialog.newInstance("请选择类型", list)
            newFragment.huidiaPosition = this::myPopSetData
            newFragment.show(content.supportFragmentManager, "missiles")
        }
    }

    /**
     * 选中类型回调1
     */
    fun myPopSetData(position: Int) {
        position1 = position
        position2 = -1
        if (position == -1) {
            return
        }

        for (i in 0 until infotype.size) {
            infotype[position].IsSelect = i == position
        }

        typeShow1.set(infotype[position])
        typeShow2.set(DialogParam("", ""))

    }


    /**
     * 品牌
     */
    fun onClickBrandname(view: View) {
        var content = view.context
        if (content is AppCompatActivity) {
            var carBrandParam = CarBrandParam()
            var intent = Intent(content, ChooseCarActivity::class.java)
            intent.putExtra("car", carBrandParam)
            intent.putExtra("notop", true)
            intent.putExtra("Isbrand", true)
            content.startActivityForResult(intent, 102)
        }


    }

    /**
     * 车系
     */
    fun onClickSeriesname(view: View) {
        var content = view.context
        if (content is AppCompatActivity) {
            var carBrandParam = CarBrandParam()
            var intent = Intent(content, ChooseCarActivity::class.java)
            intent.putExtra("car", carBrandParam)
            intent.putExtra("notop", true)
            intent.putExtra("Isseries", true)
            content.startActivityForResult(intent, 102)
        }
    }

    /**
     * 车型
     */
    fun onClickSpecname(view: View) {
        var content = view.context
        if (content is AppCompatActivity) {
            var carBrandParam = CarBrandParam()
            var intent = Intent(content, ChooseCarActivity::class.java)
            intent.putExtra("car", carBrandParam)
            intent.putExtra("notop", true)
            content.startActivityForResult(intent, 102)
        }
    }

    /**
     * 选中类型2
     */
    fun onClickType2(view: View) {
        var content = view.context

        if (content is AppCompatActivity) {

//            when (infotype[position1].key) {
//                "品牌" -> {
//                    onClickBrandname(view)
//                    return
//                }
//                "车系" -> {
//                    onClickSeriesname(view)
//                    return
//                }
//                "车型" -> {
//                    onClickSpecname(view)
//                    return
//                }
//            }

            //深拷贝
            var list = ArrayList<DialogParam>()
            var poptitle = ""
            if (position1 != -1) {

                when (infotype[position1].key) {
                    "品牌" -> {
                        onClickBrandname(view)
                        return
                    }
                    "车系" -> {
                        onClickSeriesname(view)
                        return
                    }
                    "车型" -> {
                        onClickSpecname(view)
                        return
                    }
                }

                var poplistdata = infotype[position1].poplistdata
                for (it in poplistdata) {
                    var item = DialogParam(it.name, it.id)
                    item.IsSelect = it.IsSelect
                    item.ikey = it.ikey
                    list.add(item)
                }
                poptitle = infotype[position1].poptitle
            }

            val newFragment = MyDialog.newInstance("请选择$poptitle", list)
            newFragment.huidiaPosition = this::myPopSetData2
            newFragment.show(content.supportFragmentManager, "missiles2")
        }
    }

    /**
     * 选中类型回调2
     */
    fun myPopSetData2(position: Int) {
        position2 = position
        if (position == -1) {
            return
        }

        var list = infotype[position1].poplistdata

        for (i in 0 until list.size) {
            list[position].IsSelect = i == position
        }
        typeShow2.set(list[position])
    }

    /**
     * 添加item
     */
    fun addItem(view: View) {
        var type1 = typeShow1.get()
        var type2 = typeShow2.get()
        var list = HashSet<KeyValueParam>()
        listItem.value?.apply {
            list.addAll(this)
        }
        if (list.size >= 5) {
            Toast.makeText(MyApplication.getInstance(), "最多只能增加5个条件", Toast.LENGTH_LONG).show()
            return
        }

        type1?.apply {
            var idname = this.idname
            type2?.apply {
                if (this.name == "") {
                    return
                }
                var item = if (id == "") KeyValueParam(this.name, this.name)
                else KeyValueParam(this.name, this.id)
                item.idname = idname
                list.add(item)
                listItem.postValue(list.toList())
            }
        }

    }

    /**
     * 删除item
     */
    fun onClickDel(view: View, item: KeyValueParam, position: Int) {

        var list = HashSet<KeyValueParam>()
        listItem.value?.apply {
            list.addAll(this)
            list.remove(item)
        }
        listItem.postValue(list.toList())
    }

    /**
     * 开始时间
     */
    fun startTime(view: View) {
        var content = view.context
        if (content is AppCompatActivity) {
            val datePickerFragment = DatePickerFragment()
            datePickerFragment.clickOk = this::onDateSetstart
            datePickerFragment.show(content.supportFragmentManager, "datePicker")
        }
    }

    var year1 = -1
    var month1 = -1
    var day1 = -1

    var year2 = -1
    var month2 = -1
    var day2 = -1
    var showStartT = ObservableField<String>("")
    var showStartE = ObservableField<String>("")
    /**
     * 开始时间回调
     */
    fun onDateSetstart(view: DatePicker, year: Int, month: Int, day: Int) {
        this.year1 = year
        this.month1 = month + 1
        this.day1 = day
        showStartT.set("$year1-$month1-$day1")
    }

    /**
     * 结束时间回调
     */
    fun onDateSetend(view: DatePicker, year: Int, month: Int, day: Int) {
        this.year2 = year
        this.month2 = month
        this.day2 = day
        showStartE.set("$year2-$month2-$day2")
    }

    /**
     * 结束时间
     */
    fun endTime(view: View) {
        var content = view.context
        if (content is AppCompatActivity) {
            val datePickerFragment = DatePickerFragment()
            datePickerFragment.clickOk = this::onDateSetend
            datePickerFragment.show(content.supportFragmentManager, "datePicker")
        }
    }

    /**
     * 搜索
     */
    fun searchBut(view: View) {
        var hashMap = HashMap<String, String>()
        listItem.value?.apply {
            for (i in this) {
                hashMap[i.idname] = i.value
            }
        }
        showDatetype.get()?.apply {
            hashMap["datetype"] = this.id
            showStartT.get()?.apply {
                hashMap["stime"] = this
            }
            showStartE.get()?.apply {
                hashMap["etime"] = this
            }
        }


//        var intent = Intent(view.context, SearchResultActivity::class.java)
        var intent = Intent(view.context, StockSearchResultActivity::class.java)
        intent.putExtra("hashMap", hashMap)
        view.context.startActivity(intent)
    }

    var datetypelist = arrayListOf(
            DialogParam("出厂日期", "mfddate"),
            DialogParam("生产日期", "mfgdate"),
            DialogParam("首次入库时间", "firstindate"),
            DialogParam("入库时间", "indate"),
            DialogParam("出库时间", "outdate"),
            DialogParam("扣款时间", "dedtime"),
            DialogParam("下单时间", "endtime")


    )

    var showDatetype = ObservableField<DialogParam>()
    /**
     * 点击时间类型
     */
    fun onClickDatetype(view: View) {
        var content = view.context

        if (content is AppCompatActivity) {
            //深拷贝
            var list = ArrayList<DialogParam>()
            for (it in datetypelist) {
                var item = DialogParam(it.name, it.id)
                item.IsSelect = it.IsSelect
                item.ikey = it.ikey
                list.add(item)
            }

            val newFragment = MyDialog.newInstance("请选择时间类型", list)
            newFragment.huidiaPosition = this::myPopSetDataTime
            newFragment.show(content.supportFragmentManager, "myPopSetDataTime")
        }
    }

    /**
     * 选中s时间类型回调
     */
    fun myPopSetDataTime(position: Int) {
        if (position == -1) {
            return
        }

        for (i in 0 until datetypelist.size) {
            datetypelist[position].IsSelect = i == position
        }
        showDatetype.set(datetypelist[position])
        showStartE.set("")
        showStartT.set("")

    }

    /**
     * 品牌车系回调
     */
    fun setCarData(carBrandParam: CarBrandParam) {
        when (infotype[position1].key) {
            "品牌" -> {
                typeShow2.set(DialogParam(carBrandParam.brandname, carBrandParam.brand))
            }
            "车系" -> {
                typeShow2.set(DialogParam(carBrandParam.seriesname, carBrandParam.series))
            }
            "车型" -> {
                typeShow2.set(DialogParam(carBrandParam.specname, carBrandParam.spec))
            }
        }

    }

}